package com.jy.adscm.operation.newhscrb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.newhscrb.bean.NhcMachine;
import com.jy.adscm.operation.newhscrb.bean.QueryNhcMachine;
import com.jy.adscm.operation.newhscrb.service.NhcMachineService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 耗材生产机器基本信息 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-06-18
 */
@RestController
@RequestMapping("/adscm/operation/nhcMachine")
public class NhcMachineController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    NhcMachineService nhcMachineService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody NhcMachine nhcMachine, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        nhcMachine.setCreator(token.getUser().getId().toString());
        nhcMachine.setCreateTime(new Date());
        boolean insert = nhcMachineService.insertNhcMachine(nhcMachine);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody NhcMachine nhcMachine, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        nhcMachine.setCreator(token.getUser().getId().toString());
        nhcMachine.setCreateTime(new Date());
        boolean update = nhcMachineService.updateNhcMachine(nhcMachine);
        return new SimpleCoreResponse("200","修改成功",update);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryNhcMachine queryNhcMachine, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<NhcMachine> page=nhcMachineService.select(queryNhcMachine);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectByCode")
    public SimpleCoreResponse selectByCode( String code, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<NhcMachine> nhcMachines=nhcMachineService.selectByCode(code);
        return new SimpleCoreResponse("200","查询成功",nhcMachines);
    }
}

